<?php


namespace app\home\controller;


use app\base\BaseController;
use app\home\validate\RuleCheck;
use think\exception\ValidateException;
use think\facade\Db;
use think\facade\View;

/**
 * Class MaterialType
 * @package app\home\controller
 * 物料类别
 */
class MaterialType extends BaseController
{
    protected $table='MaterialType';
protected  $title='物料类别';
    /**
     * @return \json|\think\response\View
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * date:2024/3/21
     * 列表
     */
    public function index()
    {

            if (request()->isAjax()) {
                $rule = Db::name($this->table)
                    ->where('status',1)
                    ->field('*')
                    ->select();
                $list = generateTree($rule);
                return to_assign(0, '', $list);
            } else {
                return view();
            }
    }

    /**
     * @return \json|\think\response\View
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * date:2024/3/21
     * 添加
     */
    public function add()
    {
        $param = get_params();
        if (request()->isAjax()) {
            $param['update_time']=time();
            if ($param['id'] > 0) {

                Db::name($this->table)->strict(false)->field(true)->update($param);
                add_log('edit', $param['id'], $param);
            } else {
                $param['create_time'] = time();
                $rid = Db::name($this->table)->strict(false)->field(true)->insertGetId($param);
                add_log('add', $rid, $param,$this->title);
            }
            return to_assign();
        } else {
            $id = isset($param['id']) ? $param['id'] : 0;
            $pid = isset($param['pid']) ? $param['pid'] : 0;
            if ($id > 0) {
                $detail = Db::name($this->table)->where('id', $id)->find();
                View::assign('detail', $detail);
            }
            View::assign('id', $id);
            View::assign('pid', $pid);
            return view('form');
        }
    }

    /**
     * @return \json
     * @throws \think\db\exception\DbException
     * date:2024/3/21
     * 删除
     */
    public function delete()
    {
        if (request()->isDelete()) {
            $id = get_params("id");
//            if (in_array(705,explode(',',$id))){
//                return to_assign(1, "此数据禁止删除");
//            }
            $count = Db::name($this->table)->where(["pid" => $id])->where('status','<>',-1)->count();
            if ($count > 0) {
                return to_assign(1, "该节点下还有子节点，无法删除");
            }
            try {
                Db::name($this->table)->where('id',$id)->update(['status'=>-1,'deleted'=>time()]);
                add_log('delete', $id, request()->param(),$this->title);
            }catch (\Exception $e){
                return to_assign(1, "删除失败".$e->getMessage());
            }
            return to_assign(0, "删除成功");
        } else {
            return to_assign(1, "错误的请求");
        }
    }
}